﻿#include "YMuxerTask.h"
#include <iostream>

using namespace std;

extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
}


void YMuxerTask::Do(AVPacket* pkt)
{
    pkts_.Push(pkt);
    Next(pkt);
}
void YMuxerTask::Working()
{
    muxer_.WriteHead();
    while (!isExit_)
    {
        unique_lock<mutex> lock(mux_);
        auto pkt = pkts_.Pop();
        if (!pkt)
        {
            MSleep(1);
            continue;
        }

        muxer_.Write(pkt);
        cout << "W" << flush;

        av_packet_free(&pkt);
    }
    muxer_.WriteEnd();
    muxer_.Close();
}
bool YMuxerTask::Open(const char* url,
    AVCodecParameters* video_para,
    AVRational* video_time_base,
    AVCodecParameters* audio_para,
    AVRational* audio_time_base
)
{
    auto c = muxer_.Open(url, video_para, audio_para);
    if (!c)return false;
    muxer_.setContext(c);
    muxer_.set_src_video_time_base(video_time_base);
    muxer_.set_src_audio_time_base(audio_time_base);
    return true;
}


YMuxerTask::YMuxerTask()
{
}

YMuxerTask::~YMuxerTask()
{
}
